{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2b3a83fe-edf2-45b7-8b76-af2324296ad0",
   "metadata": {},
   "source": [
    "### Import API Keys and Establish Connections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bacb0c55-44ee-4505-a3bc-7aaa3d72b28b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv\n",
    "from openai import OpenAI\n",
    "import ollama\n",
    "import anthropic\n",
    "from IPython.display import Markdown, display, update_display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1767187f-c065-43df-b778-fcd48bd5e48d",
   "metadata": {},
   "outputs": [],
   "source": [
    "load_dotenv(override=True)\n",
    "openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "google_api_key = os.getenv(\"GOOGLE_API_KEY\")\n",
    "anthropic_api_key = os.getenv(\"ANTHROPIC_API_KEY\")\n",
    "\n",
    "if openai_api_key:\n",
    "    print(f\"OpenAI API key exists {openai_api_key[:8]}\")\n",
    "else:\n",
    "    print(f\"OpenAI API key not set\")\n",
    "\n",
    "if google_api_key:\n",
    "    print(f\"Google API key exists {google_api_key[:7]}\")\n",
    "else:\n",
    "    print(f\"Google API key not set\")\n",
    "\n",
    "if anthropic_api_key:\n",
    "    print(f\"Anthropic API key exists {openai_api_key[:8]}\")\n",
    "else:\n",
    "    print(f\"Anthropic API key not set\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc7ca3ab-ff7f-4375-bcad-aca49c7f4f4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initializing API Clients, loading the SDKs\n",
    "# An SDK is a library/toolbox (Pre-built functions, classes, utilities) full \n",
    "# of everything you need to use someone else's software\n",
    " \n",
    "openai = OpenAI()\n",
    "claude = anthropic.Anthropic()\n",
    "ollama_via_openai = OpenAI(base_url='http://localhost:11434/v1', api_key = 'ollama')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81e01904-5586-4726-ab91-7bdbd6bde6d9",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### A Coversation between 3 chatbots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "843bbb69-ab7d-4b13-b878-65a4275f53ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Conversation between GPT-4o-mini, Claude-3, ang Gemini 2.5 flash\n",
    "\n",
    "gpt_model = \"gpt-4o-mini\"\n",
    "claude_model = \"claude-3-haiku-20240307\"\n",
    "ollama_model = \"llama3.2\"\n",
    "\n",
    "gpt_system = \"You are an eternal optimist. You always see the bright side of things and believe even \\\n",
    "simple actions have deep purpose. Keep replies under 2 sentences.\"\n",
    "\n",
    "ollama_system = \"You are a witty skeptic who questions everything. You tend to doubt grand explanations \\\n",
    "and prefer clever, sarcastic, or literal answers. Keep replies under 2 sentences.\"\n",
    "\n",
    "claude_system = \"You are a thoughtful philosopher. You consider all perspectives and enjoy finding \\\n",
    "symbolic or existential meaning in simple actions. Keep replies under 2 sentences.\"\n",
    "\n",
    "\n",
    "gpt_messages = [\"Hi! Todays topic for discussion is 'Why did the chicken cross the road?'\"]\n",
    "ollama_messages = [\"That's quite the topic. \"]\n",
    "claude_messages = [\"Lets begin our discussion.\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a4da2f5-ff74-4847-aa86-867e89173509",
   "metadata": {},
   "outputs": [],
   "source": [
    "def call_gpt():\n",
    "    \n",
    "    messages = [{\"role\":\"system\", \"content\":gpt_system}]\n",
    "    \n",
    "    for gpt, ollama, claude in zip(gpt_messages, ollama_messages, claude_messages):\n",
    "        messages.append({\"role\": \"assistant\", \"content\": gpt})\n",
    "        messages.append({\"role\": \"user\", \"content\": ollama})\n",
    "        messages.append({\"role\": \"user\", \"content\": claude})\n",
    "    \n",
    "    response = openai.chat.completions.create(\n",
    "        model = gpt_model,\n",
    "        messages = messages,\n",
    "        max_tokens = 500\n",
    "    )\n",
    "    return response.choices[0].message.content.strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5848d83a-f4aa-42ee-b40b-6130da60c890",
   "metadata": {},
   "outputs": [],
   "source": [
    "def call_ollama():\n",
    "    messages = [{\"role\":\"system\", \"content\":ollama_system}]\n",
    "    \n",
    "    for gpt, ollama_message, claude in zip(gpt_messages, ollama_messages, claude_messages):\n",
    "        messages.append({\"role\": \"user\", \"content\": gpt})\n",
    "        messages.append({\"role\": \"assistant\", \"content\": ollama_message})\n",
    "        messages.append({\"role\": \"user\", \"content\": claude})\n",
    "    \n",
    "    messages.append({\"role\":\"user\", \"content\": gpt_messages[-1]})\n",
    "\n",
    "    response = ollama_via_openai.chat.completions.create(\n",
    "            model = ollama_model,\n",
    "            messages = messages\n",
    "    )\n",
    "    return response.choices[0].message.content.strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a50e4f7c-d594-4ed8-a658-2d8b2fde21a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def call_claude():\n",
    "    \n",
    "    messages = []\n",
    "    \n",
    "    for gpt, ollama, claude_message in zip(gpt_messages, ollama_messages, claude_messages):\n",
    "        messages.append({\"role\":\"user\", \"content\":gpt})\n",
    "        messages.append({\"role\": \"user\", \"content\": ollama})\n",
    "        messages.append({\"role\":\"assistant\", \"content\": claude_message})\n",
    "    \n",
    "    messages.append({\"role\": \"user\", \"content\": gpt_messages[-1]})\n",
    "    messages.append({\"role\": \"user\", \"content\": ollama_messages[-1]})\n",
    "    \n",
    "    response = claude.messages.create(\n",
    "        model = claude_model,\n",
    "        system = claude_system,\n",
    "        messages = messages,\n",
    "        max_tokens = 500\n",
    "    )\n",
    "    return response.content[0].text.strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c78fcf8-544e-413f-af18-ccb9000515de",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(f\"GPT:\\n{gpt_messages[0]}\\n\")\n",
    "print(f\"Ollama:\\n{ollama_messages[0]}\\n\")\n",
    "print(f\"Claude:\\n{claude_messages[0]}\\n\")\n",
    "\n",
    "for i in range(5):\n",
    "    gpt_next = call_gpt()\n",
    "    print(f\"GPT: \\n{gpt_next}\\n\")\n",
    "    gpt_messages.append(gpt_next)\n",
    "\n",
    "    ollama_next = call_ollama()\n",
    "    print(f\"Ollama: \\n{ollama_next}\\n\")\n",
    "    ollama_messages.append(ollama_next)\n",
    "    \n",
    "    claude_next = call_claude()\n",
    "    print(f\"Claude: \\n{claude_next}\\n\")\n",
    "    claude_messages.append(claude_next)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ea7419a-ea8f-42da-a9a1-4bbe5342cecb",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### Another Coversation between 3 chatbots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c279c275-7b95-4587-9cc6-4d32517ec253",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Conversation between GPT-4o-mini, Claude-3, ang Gemini 2.5 flash\n",
    "\n",
    "gpt_model = \"gpt-4o-mini\"\n",
    "claude_model = \"claude-3-haiku-20240307\"\n",
    "ollama_model = \"llama3.2\"\n",
    "\n",
    "gpt_system = \"You are an optimist who believes technology brings people \\\n",
    "closer together and improves lives. Defend innovation as a force for human \\\n",
    "connection. Keep response under 3 sentences.\"\n",
    "\n",
    "\n",
    "ollama_system = \"You are a skeptic who questions if technology isolates us \\\n",
    "and worsens social divides. Highlight its risks and unintended consequences. \\\n",
    "Keep response under 3 sentences.\"\n",
    "\n",
    "\n",
    "claude_system = \"You are a philosopher who explores both sides \\\n",
    "of technology's impact. Seek a balanced perspective on connection and isolation.\\\n",
    "Keep response under 3 sentences.\"\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "gpt_messages = [\"Our topic of discussion for today will be: 'Is technology making us more connected or more isolated?'\"]\n",
    "ollama_messages = [\"A great topic\"]\n",
    "claude_messages = [\"Let's begin.\"]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44c023a6-f22f-4a64-a718-f75fe4c8233a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def call_gpt():\n",
    "    \n",
    "    messages = [{\"role\":\"system\", \"content\":gpt_system}]\n",
    "    \n",
    "    for gpt, ollama, claude in zip(gpt_messages, ollama_messages, claude_messages):\n",
    "        messages.append({\"role\": \"assistant\", \"content\": gpt})\n",
    "        messages.append({\"role\": \"user\", \"content\": ollama})\n",
    "        messages.append({\"role\": \"user\", \"content\": claude})\n",
    "    \n",
    "    response = openai.chat.completions.create(\n",
    "        model = gpt_model,\n",
    "        messages = messages,\n",
    "        max_tokens = 500\n",
    "    )\n",
    "    return response.choices[0].message.content.strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d29f27a1-457e-4e71-88dc-c55e4a36a27c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def call_ollama():\n",
    "    messages = [{\"role\":\"system\", \"content\":ollama_system}]\n",
    "    \n",
    "    for gpt, ollama_message, claude in zip(gpt_messages, ollama_messages, claude_messages):\n",
    "        messages.append({\"role\": \"user\", \"content\": gpt})\n",
    "        messages.append({\"role\": \"assistant\", \"content\": ollama_message})\n",
    "        messages.append({\"role\": \"user\", \"content\": claude})\n",
    "    \n",
    "    messages.append({\"role\":\"user\", \"content\": gpt_messages[-1]})\n",
    "\n",
    "    response = ollama_via_openai.chat.completions.create(\n",
    "            model = ollama_model,\n",
    "            messages = messages\n",
    "    )\n",
    "    return response.choices[0].message.content.strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69577edc-4be2-40fc-8eac-1243c30cda26",
   "metadata": {},
   "outputs": [],
   "source": [
    "def call_claude():\n",
    "    \n",
    "    messages = []\n",
    "    \n",
    "    for gpt, ollama, claude_message in zip(gpt_messages, ollama_messages, claude_messages):\n",
    "        messages.append({\"role\":\"user\", \"content\":gpt})\n",
    "        messages.append({\"role\": \"user\", \"content\": ollama})\n",
    "        messages.append({\"role\":\"assistant\", \"content\": claude_message})\n",
    "    \n",
    "    messages.append({\"role\": \"user\", \"content\": gpt_messages[-1]})\n",
    "    messages.append({\"role\": \"user\", \"content\": ollama_messages[-1]})\n",
    "    \n",
    "    response = claude.messages.create(\n",
    "        model = claude_model,\n",
    "        system = claude_system,\n",
    "        messages = messages,\n",
    "        max_tokens = 500\n",
    "    )\n",
    "    return response.content[0].text.strip()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acedf2fb-8b20-49be-9a80-24fb3896e2ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(f\"GPT:\\n{gpt_messages[0]}\\n\")\n",
    "print(f\"Ollama:\\n{ollama_messages[0]}\\n\")\n",
    "print(f\"Claude:\\n{claude_messages[0]}\\n\")\n",
    "\n",
    "for i in range(5):\n",
    "    gpt_next = call_gpt()\n",
    "    print(f\"GPT: \\n{gpt_next}\\n\")\n",
    "    gpt_messages.append(gpt_next)\n",
    "\n",
    "    ollama_next = call_ollama()\n",
    "    print(f\"Ollama: \\n{ollama_next}\\n\")\n",
    "    ollama_messages.append(ollama_next)\n",
    "    \n",
    "    claude_next = call_claude()\n",
    "    print(f\"Claude: \\n{claude_next}\\n\")\n",
    "    claude_messages.append(claude_next)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a078943b-7a34-4697-b1f6-16f4b0e7aed6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
